package com.slz3.business.module.orderManagement.ordOrder.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.orderManagement.ordOrder.domain.OrdOrder;
import com.slz3.business.module.orderManagement.ordOrder.domain.OrdOrderFactory;
import com.slz3.business.module.orderManagement.ordOrder.steward.OrdOrderSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;

@Slf4j
@Service
public class OrdOrderServiceImpl implements OrdOrderService {

    @Override
    public OrdOrder save(OrdOrder ordOrder) {
        return Repositorys.ordOrder.save(OrdOrderFactory.initBaseFields(ordOrder));
    }

    @Override
    public Page<OrdOrder> page(MultiValueMap<String, String> params) {
        Page<OrdOrder> ordOrderPage = Repositorys.ordOrder.findAll(
                OrdOrderSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return ordOrderPage;
    }

    @Override
    public OrdOrder modify(OrdOrder ordOrderNew) {
        return Repositorys.ordOrder.saveAndFlush(OrdOrderFactory.initBaseFields(ordOrderNew));
    }

    @Override
    public Optional<OrdOrder> single(UUID uuid) {
        return Repositorys.ordOrder.findById(uuid);
    }

    @Override
    public List<OrdOrder> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.ordOrder.findAllById(uuids);
    }

    @Override
    public List<OrdOrder> delete(List<OrdOrder> ordOrderList) {
        ordOrderList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            OrdOrderFactory.initBaseFields(item);
        });
        return Services.ordOrderTransactional.batchModify(ordOrderList);
    }

}
